<template lang="pug">
	.health-index
		swiper.slider-wrap(indicator-dots="true",previous-margin="40rpx",next-margin="40rpx",indicator-color="rgba(255, 255, 255, .5)",indicator-active-color="#FFFFFF" @change="swiperTab")
			block(v-for="(item,index) in swiperList" :key="item")
				swiper-item.slider-item
					.slide-no-card(v-if="item.type === 'no-card' && swiperList.length<2" @click="getHealthCard")
						.receive-card + 领取电子健康卡
					.slide-add-card(v-if="item.type === 'add-card' && swiperList.length>1" @click="getHealthCard")
						.receive-card + 领取电子健康卡
					.slide-card(v-if="!item.type && swiperList.length>1" @click="goPage('isHaveCard','/views/healthCard/qrcode')")
						.user-info
							.name
								| {{item.name}}
								.state(v-if="item.realState==='0'") (未认证)
							.info-content
								.div 身份证：{{item.idNumberText}}
								.div 手机号：{{item.phoneText}}
						.qrcode-info
							.qrcode-box
								img(src="../../assets/image/healthCard/qrcode.png")
							.hint 点击出示二维码
		.module-wrap
			.module-item(v-for="(item,index) in moduleList" :key="item" @click="goPage(item.option,item.path,item.name)")
				.module-icon
					img(:src="item.img")
				.module-name {{item.name}}
		.bind-app
			.field-wrap(@click="goOrder")
				field-item(text="我的预约" :img="yuyue" :showArrow="true")
			.field-wrap(@click="goAccount")
				field-item(text="账户中心" :img="zhanghu" :showArrow="true" :showBorder="false")
</template>
<script>
import fieldItem from '@/components/field-item'
import guahao from '@/assets/image/common/s_menu_yuyue.png'
import jiuzhen from '@/assets/image/common/s_menu_zhangdan.png'
import zhuyuan from '@/assets/image/common/s_menu_zhuyuan.png'
import baogao from '@/assets/image/common/s_menu_baogao.png'
import fapiao from '@/assets/image/common/s_menu_fapiao.png'
import guanli from '@/assets/image/common/s_menu_jiuzhenka.png'
import yuyue from '@/assets/image/healthCard/u_menu_yuyue.png'
import zhanghu from '@/assets/image/healthCard/u_menu_setting.png'
import commonMethod from '@/assets/js/commonMethod.js'
export default {
	data() {
		return {
			yuyue: yuyue,
			zhanghu: zhanghu,
			swiperList: [{ type: 'no-card' }],
			selfList: {},
			currentTab: 0,
			moduleList: [],
			selfRealState: false, //判断本人是否认证
			selfHaveCard: false //判断本人是否绑卡
		}
	},
	computed: {},
	onLoad() {
		Object.assign(this.$data, this.$options.data()) //进入页面数据重置
	},
	mounted() {
		this.moduleList = [
			{
				img: guahao,
				name: '预约挂号',
				option: 'isHaveCard',
				path: '/views/bespeak/index'
			},
			{
				img: jiuzhen,
				name: '就诊记录',
				option: 'isHaveCard',
				path: '/views/doctorRecord/index'
			},
			{
				img: zhuyuan,
				name: '住院记录',
				option: 'isHaveCard',
				path: '/views/hospitalRecord/index'
			},
			{
				img: baogao,
				name: '报告查询',
				option: 'isRealState',
				path: '/views/report/index'
			},
			{
				img: fapiao,
				name: '电子发票',
				option: 'isHaveCard',
				path: '/views/invoice/index'
			},
			{
				img: guanli,
				name: '就诊卡管理',
				option: 'isHaveCard',
				path: '/views/cardManage/index'
			}
		]
	},
	onShow() {
		if (commonMethod.isLogin) {
			this.getCardList()
		} else {
			this.wx.showToast({
				title: '您未登录，请先登录',
				icon: 'none'
			})
			setTimeout(() => {
				that.$router.push({
					path: '/views/login/login'
				})
			}, 1000)
		}
	},
	methods: {
		swiperTab(e) {
			this.currentTab = e.target.current
		},
		getCardList() {
			let selfList = {}
			this.api.tzApi
				.getCardList({
					mainHealthId: this.wx.getStorageSync('userInfo').memberNum
				})
				.then(data => {
					if (data.responseFlag === '1') {
						let result = data.data || []
						if (result.length <= 0) {
							result.push({ type: 'no-card' })
						} else {
							selfList =
								result.find(item => item.familyRelation === '本人') || {}
							result = _.uniq([selfList, ...result])
							result.forEach(item => {
								item.idNumberText = this.tools.hideNum(item.idNumber, 4, 4)
								item.phoneText = this.tools.hideNum(item.phone, 3, 4)
							})
							result.push({ type: 'add-card' })
						}
						this.selfList = selfList
						this.swiperList = result
					}
				})
				.catch(() => {})
		},
		getHealthCard() {
			this.wx.setStorage({
				//缓存本人信息
				key: 'familyRole',
				data: {
					name: this.selfList.name,
					idNumber: this.selfList.idNumber,
					familyRelation: this.selfList.familyRelation
				}
			})
			this.$router.push({
				path: '/views/healthCard/getHealthCard',
				query: {
					isReadFamily: this.selfList.list === null ? '1' : '0' //本人未领卡下，新生儿领取选项跳转不同
				}
			})
		},
		goPage(option, path, name) {
			let data,
				that = this
			let currentSwiper = this.swiperList[this.currentTab]
			let familyRealState = currentSwiper.realState === '1'
			let familyHaveCard = currentSwiper.list !== null
			let selfRealState = this.selfList.realState === '1'
			let selfHaveCard = this.selfList.list !== null

			if (option === 'isRealState' && !familyRealState) {
				//家人未认证未绑卡本人未认证，先认证本人
				if (!familyHaveCard && !selfRealState) {
					data = {
						name: this.selfList.name,
						idNumber: this.selfList.idNumber,
						familyRelation: this.selfList.familyRelation
					}
				}
				//家人未认证已绑卡，认证家人
				if (familyHaveCard) {
					data = {
						name: currentSwiper.name,
						idNumber: currentSwiper.idNumber,
						familyRelation: currentSwiper.familyRelation
					}
				}
				this.wx.setStorage({
					key: 'familyRole',
					data: data
				})
				this.wx.showModal({
					title: '提示',
					content: '就医过程需实名认证，请先认证您的个人信息',
					confirmText: '去认证',
					success(res) {
						res.confirm &&
							that.$router.push({
								path: '/views/healthCard/insertId',
								query: {
									isReadFamily: '1', //是否需要读取家人信息
									authentication: '1' //是否去认证
								}
							})
					}
				})
				return false
			}

			if (option === 'isHaveCard' && !familyHaveCard) {
				this.wx.showModal({
					title: '提示',
					content: '您还未开通电子健康卡，建议您前往开通全市就医更便捷',
					cancelText: '下次再说',
					confirmText: '立即开通',
					success(res) {
						if (res.confirm) {
							that.wx.setStorage({
								key: 'familyRole',
								data: {
									name: currentSwiper.name,
									idNumber: currentSwiper.idNumber,
									familyRelation: currentSwiper.familyRelation
								}
							})
							let query = familyRealState
								? {
										isQuickGet: '1' //是否快捷领取电子健康卡
								  }
								: {
										isReadFamily: '1',
										isNormalGet: '1' //是否去领卡
								  }
							that.$router.push({
								path: '/views/healthCard/getHealthCard',
								query: query
							})

							// if (familyRealState) {
							// 	//快捷领卡
							// 	that.$router.push({
							// 		path: '/views/healthCard/getHealthCard',
							// 		query: {
							// 			realState: true
							// 		}
							// 	})
							// } else {
							// 	that.$router.push({
							// 		path: '/views/healthCard/getHealthCard',
							// 		query: {
							// 			isReadFamily: '1',
							// 			getCard: true
							// 		}
							// 	})
							// }
						}
					}
				})
				return false
			}

			if (path.indexOf('/views/') !== 0) {
				//跳转webview
				this.wx.setStorage({
					key: 'jumpUrl',
					data: {
						url: path,
						title: name,
						params: {
							memberNum: this.wx.getStorageSync('userInfo').memberNum,
							accessToken: this.wx.getStorageSync('userInfo').accessToken,
							name: this.atob(currentSwiper.name),
							idNumber: this.atob(currentSwiper.idNumber)
						}
					}
				})
				this.$router.push({
					path: '/views/webview/webview'
				})
				return false
			}

			if (path.indexOf('doctorRecord') > 0) {
				//进入就诊记录
				let currentCard = currentSwiper.list[0]
				this.wx.setStorage({
					key: 'findPatient',
					data: {
						name: currentCard.name,
						idNumber: currentCard.idNumber,
						cardType: currentCard.cardType,
						cardNumber: currentCard.cardNumber,
						sscardId: currentCard.sscardId,
						phone: currentCard.phoneNumber,
						yyzt: currentCard.yyzt
					}
				})
			}

			this.$router.push({
				path: path
			})
		},
		goOrder() {
			this.$router.push({
				path: '/views/myOrder/index'
			})
		},
		goAccount() {
			this.$router.push({
				path: '/views/my/account'
			})
		},
		wxLogin() {
			this.wx.login({
				success(res) {
					if (res.code) {
						console.log(res.code)
					} else {
					}
				}
			})
		}
	},
	components: {
		fieldItem
	}
}
</script>

<style lang="stylus">
	.health-index
		.slider-wrap
			background-color themeColor
			height 460px
			padding-bottom 10px
			padding-top 20px
			.slider-item
				width: 100%
				display flex
				justify-content center
				align-items top
				> div
					border-radius 25px
					overflow hidden
					width 640px
					height 400px
					box-shadow 0px 6px 12px rgba(0,0,0,0.1)
				.slide-no-card,.slide-card
					centerBackground("../../assets/image/healthCard/card.png")
				.slide-no-card,.slide-add-card
					line-height 400px
					text-align center
					.receive-card
						font-size 36px
						font-weight 500
						color white
				.slide-no-card
					.receive-card
						background rgba(0,0,0,0.3)
				.slide-add-card
					background rgba(255,255,255,0.3)
				.slide-card
					display flex
					justify-content space-around
					align-items center
					.user-info
						color blackFontColor
						.name
							font-size 36px
							font-weight 700
							display flex
						.info-content
							margin-top 20px
							font-size 28px
							line-height 1.5
					.qrcode-info
						text-align center
						.qrcode-box
							display inline-block
							background-color white
							padding 7px
							border-radius 10px
							img
								width 86px
								height 86px
						.hint
							font-size 24px
							margin-top 10px
		.module-wrap
			background-color white
			padding 40px 25px
			display flex
			flex-wrap wrap
			.module-item
				width 33%
				padding 30px 0
				text-align center
				.module-icon
					img
						width 62px
						height 62px
				.module-name
					font-size 28px
					margin-top 15px
		.bind-app
			margin-top 20px
</style>
